<?php
namespace App\Model;

use App\Repository\Sys\Db;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

class UserTable
{
    use Db;
    static private $table = 'user';

    //用户-列表-admin
    static public function getUserList($page, $limit, $title)
    {
//        $page = \Request::get('page', 1);
//        $limit = \Request::get('page_num', 10);
//        $title = \Request::get('title');
        $param = [];
        $where = '1=1';
        if ($title) {
            $where .= " AND (nickname LIKE :nickname or phone LIKE :phone)";
            $param['nickname'] = "%{$title}%";
            $param['phone'] = "%{$title}%";
        }
        $count = \DB::select("select count(*) as c from user WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from user WHERE {$where} ORDER BY id DESC limit :offset,:limit", $param);
        $list = [];
        foreach ($res as $v) {

//            UserLevelSetTable::updateLevel($v->id, $v->integral);  //更新会员等级与积分


            $v->num_booking = 1;//订场次数
            $v->num_cancel = 2;//取消次数
            $v->vip_text = $v->vip;//vip等级文字

            $team_res=TeamTable::ORM()->where('admin_id', $v->id)->where('status', 1)->first();
            $v->team_id_text = $team_res?$team_res->name:0;;// 所属球队名称
            $v->pwd = '';//密码
            $v->status_text = ConstTable::STATUS_NAME[$v->status];

            $v->birthday=date('Y-m-d',$v->birthday);
            $v->reg_time=date('Y-m-d',$v->reg_time);
            $list[] = $v;
        }
        $data = [
            'count' => $count,
            'list' => $list,
            'page' => $page,

        ];
        return JsonRespose::jsonArray(1, $data);
    }

    //用户-详情-通用
    static public function getUserInfo($id)
    {
        $res = UserTable::ORM()->where('id', $id)->first();

        if($res->status!=1){
            return JsonRespose::jsonArray(0,'用户已禁用');
        }


        if (!$res) {
            return JsonRespose::jsonArray(0,[],'数据不存在');
        }
        if (!$res->img) {
            $res->img = '/wap/img/login_img.png';
        }

        $res->pwd = '';
        $res->age = Str::getAge($res->birthday);
        $res->qrcode = '/phpqrcode/index.php?str=' . $res->phone;
        $res->birthday = date("Y-m-d",$res->birthday);

        $team_res=TeamTable::ORM()->where('admin_id', $id)->where('status', 1)->first();
        $res->team_admin =$team_res?$team_res->id:0;
        
        
        $res->num_booking = OrderTable::ORM()->where('uid', $id)->where('order_type', 3)->where('pay_type', 2)->count();  //订场次数
        $res->num_cancel = OrderTable::ORM()->where('uid', $id)->where('order_type', 3)->where('pay_type', 3)->count();  //订场次数;   //num_cancel 取消次数

        $res->team_list = TeamTable::getMyTeamList($res->id)['data'];

        $res->reg_time =  date("Y-m-d",$res->reg_time);



        return JsonRespose::jsonArray(1, $res);
    }

    //用户-保存-wap
    static public function postUserSave($id, $img, $birthday, $nickname,$age)
    {

        $data=[
            'nickname' => $nickname,
            'birthday' => $birthday,
            'age'=>$age,
        ];

        if($img){
            $data['img']=  $img;
        }
        $res = UserTable::ORM()->where('id', $id)->update($data);


        return JsonRespose::jsonArray(1, $res);
    }


    static public function getUserName($id)
    {
        $res = UserTable::ORM()->where('id', $id)->first();
        if ($res->nickname != null) {
            return $res->nickname;
        } else {
            return $res->phone;
        }
    }

    static public function integralList($uid,$page,$limit)
    {
        $user_res = UserTable::ORM()->where('id', $uid)->first();
        $count = \DB::select("select count(*) as c  from `order` WHERE pay_type=2 AND uid={$uid}", [])[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from `order` WHERE  pay_type=2 AND uid={$uid} ORDER BY order_id DESC limit :offset,:limit", $param);

        $data = [];
        foreach ($res as $v) {

            if ($v->order_type == 1) {
                $desc = '约战';
            }

            if ($v->order_type == 2) {
                $desc = '应战';
            }

            if ($v->order_type == 3) {
                $desc = '预定场地';
            }

            $data[] = [
                'integral' => (int)$v->money,
                'dateline' => date('Y-m-d H:i', $v->dateline),
                'desc' => $desc,
                'money'=>$v->money,
                'order_id'=>$v->order_id
            ];
        }

        $d = [
            'count' => $count,
            'integral' => $user_res->integral,
            'list' => $data,
            'page' => $page,

        ];
        return JsonRespose::jsonArray(1,$d);
    }
}